package com.laiketui.common.mapper;

import com.laiketui.domain.order.BargainOrderModel;
import com.laiketui.core.db.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author Trick
 */
public interface BargainOrderModelMapper extends BaseMapper<BargainOrderModel> {


    /**
     * 砍价是否失败
     *
     * @param orderno -
     * @param endTime -
     * @return int
     * @author Trick
     * @date 2021/4/16 16:36
     */
    @Select("select count(1) from lkt_bargain_order where order_no=#{orderno} and achievetime>#{endDate}")
    int isBargainEnd(String orderno, long endTime);

    /**
     * 获取用户参与的砍价列表
     *
     * @param userId -
     * @param start  -
     * @param size   -
     * @return List
     * @author Trick
     * @date 2021/4/16 17:31
     */
    @Select("select o.*,g.end_time from lkt_bargain_order as o left join lkt_bargain_goods as g on g.id = o.bargain_id " +
            " where user_id = #{userId} and g.is_delete = 0 order by o.status desc limit #{start},#{size}")
    List<Map<String, Object>> getUserBargainList(String userId, int start, int size);


    /**
     * 获取用户最新砍价订单信息
     *
     * @param storeId   -
     * @param bargainId -
     * @param userId    -
     * @param goodsId   -
     * @param attrId    -
     * @return BargainOrderModel
     * @author Trick
     * @date 2021/4/19 9:48
     */
    @Select("select * from lkt_bargain_order where store_id=#{storeId} and bargain_id=#{bargainId} and user_id=#{userId} order by addtime desc  limit 0,1")
    BargainOrderModel getUserBargainOrder(int storeId, int bargainId, String userId, int goodsId, int attrId);

    /**
     * 砍价成功
     *
     * @param storeId  -
     * @param orderno  -
     * @param oriPrice -
     * @param sysTime  -
     * @return int
     * @author Trick
     * @date 2021/4/20 11:25
     */
    @Update("update lkt_bargain_order set original_price = 0,status=1, achievetime = #{sysTime} where store_id = #{storeId} and order_no=#{orderno}")
    int bargainSuccess(int storeId, String orderno, long sysTime);


    /**
     * 修改砍价剩
     *
     * @param storeId  -
     * @param orderno  -
     * @param oriPrice -
     * @param sysTime  -
     * @return int
     * @author Trick
     * @date 2021/4/20 11:25
     */
    @Update("update lkt_bargain_order set original_price = original_price - #{oriPrice} , achievetime = #{sysTime} where store_id = #{storeId} and order_no=#{orderno}")
    int updateOriginalPrice(int storeId, String orderno, BigDecimal oriPrice, long sysTime);


    /**
     * 获取砍价详情
     *
     * @param map -
     * @return List
     * @author Trick
     * @date 2021/5/13 17:01
     */
    List<Map<String, Object>> getUserBargainInfo(Map<String, Object> map);
}